package Client.GUI;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.sql.rowset.serial.SerialException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import Client.logic.ClientIF;
import Server.DataBase.GradeTask;
import Server.DataBase.Reg;
import Server.DataBase.Semester;
import Server.DataBase.User;
import Server.GUI.MyJTable;
import Server.Message.MessageGetFile;
import Server.Message.MessageGetGradeTaskFile;
import Server.Message.MessagePfr;
import Server.Message.MessagePfrReplay;
import Server.Message.MessagePfs;
import Server.Message.MessagePfsReplay;
import Server.Message.MessagePft;
import Server.Message.MessagePftReplay;



public class PersonalFilePanel extends MyJPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JComboBox comboBoxSemester;
	private MyJTable regTable, taskTable;
	private JButton btnDownload;
	private JTextPane textPane, textPane_1; 
	private JButton btnReset;
	private Semester semester;
	private Reg reg;
	private GradeTask task;
	private ArrayList<Reg> regArray;
	private ArrayList<GradeTask> taskArray;
	private JFileChooser chooser;
	private String dir;
	private User user;
	
	public PersonalFilePanel(ClientIF client, User user) {
		super(PanelType.PERSONAL_FILE_PANEL, client);
		setLayout(null);
		
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"student personal file.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(91, 11, 635, 79);
			add(lblAddClass);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		this.user = user;
		init();
	}
	
	private void initLabels(){
		
		JLabel lblSemester = new JLabel("Semester:");
		lblSemester.setBounds(237, 124, 85, 14);
		add(lblSemester);
		
		JLabel lblCourse = new JLabel("Course:");
		lblCourse.setBounds(10, 169, 60, 27);
		add(lblCourse);
		

		JLabel lblTask = new JLabel("Task:");
		lblTask.setBounds(10, 324, 46, 14);
		add(lblTask);
		
		JLabel lblComments = new JLabel("Comments:");
		lblComments.setBounds(534, 175, 92, 14);
		add(lblComments);
		
		JLabel lblComments_1 = new JLabel("Comments:");
		lblComments_1.setBounds(534, 324, 92, 14);
		add(lblComments_1);
		
		textPane = new JTextPane();
		textPane.setEditable(false);
		textPane.setBorder(new LineBorder(new Color(0, 0, 0)));
		textPane.setBounds(626, 169, 199, 117);
		add(textPane);
		
		textPane_1 = new JTextPane();
		textPane_1.setEditable(false);
		textPane_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		textPane_1.setBounds(626, 324, 199, 128);
		add(textPane_1);
	
	}
	
	private void initComboBox(){
		
		comboBoxSemester = new JComboBox();
		comboBoxSemester.setBounds(303, 121, 112, 20);
		add(comboBoxSemester);
	}
	
	private void initTables(){
		regTable = new MyJTable();
		regTable.setBounds(72, 169, 452, 128);
		regTable.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		add(regTable);
		
		taskTable = new MyJTable();
		taskTable.setBounds(72, 324, 452, 128);
		taskTable.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		add(taskTable);
	}
	
	private void initBtn(){
		btnDownload = new JButton("Download");
		btnDownload.setBounds(242, 475, 130, 23);
		btnDownload.setEnabled(false);
		btnDownload.addActionListener(new FileChooser());
		add(btnDownload);
		
		btnReset = new JButton("Reset");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getClient().swapFromBack(pushPanel());
			}
		});
		btnReset.setBounds(473, 120, 89, 23);
		add(btnReset);
	}
	
	public void init(){
		initComboBox();
		initLabels();
		initTables();
		initBtn();
		
		getClient().sendMsgToServer(new MessagePfs(user.getIdUser()));
		MessagePfsReplay msg =  (MessagePfsReplay) getClient().getMessageFromServer();
		comboBoxSemester.addItem("Choose");
		for (Semester sem : msg.getArray()) comboBoxSemester.addItem(sem);
		comboBoxSemester.addActionListener(new SemesterListener());
		
	}
	
	
	
	private class SemesterListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			semester = (Semester) comboBoxSemester.getSelectedItem();	
			
			getClient().sendMsgToServer(new MessagePfr(user.getIdUser(), semester.getIdSemester()));
			MessagePfrReplay msg =  (MessagePfrReplay) getClient().getMessageFromServer();
			
			CoursesTableModel ctm = new CoursesTableModel();
			regArray = msg.getArray();
			ctm.setArray(regArray);
			regTable.setModel(ctm);
			comboBoxSemester.setEnabled(false);
			regTable.getTable().getSelectionModel().addListSelectionListener(new RegListener());
		}
	}
	
	private class RegListener implements ListSelectionListener{
		public void valueChanged(ListSelectionEvent arg0) {
			try{
				int i = regTable.getSelectedItem();	
				reg = regArray.get(i);
				textPane.setText(reg.getComments());
			
				getClient().sendMsgToServer(new MessagePft(user.getIdUser(), reg.getIdLesson()));
				MessagePftReplay msg =  (MessagePftReplay) getClient().getMessageFromServer();
				taskArray = msg.getArray();
				TaskTableModel ttm = new TaskTableModel();
				ttm.setArray(taskArray);
				taskTable.setModel(ttm);
				taskTable.getTable().getSelectionModel().addListSelectionListener(new TaskListener());
			} catch (java.lang.Throwable t){
				reg=null;
			}
		}
	}
	
	private class TaskListener implements ListSelectionListener{
		public void valueChanged(ListSelectionEvent arg0) {
			try{
				int i = taskTable.getSelectedItem();
				task = taskArray.get(i);
				textPane_1.setText(task.getComments());
				btnDownload.setEnabled(true);
			}
			catch (java.lang.Throwable t){
				task = null;
			}
		}
	}
	
	private class FileChooser implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			chooser = new JFileChooser(); 
			chooser.setCurrentDirectory(new java.io.File("."));
			chooser.setDialogTitle("Choose Folder");
			chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		    //
		    // disable the "All files" option.
		    //
			chooser.setAcceptAllFileFilterUsed(false);
		    //    
		    if (chooser.showSaveDialog((Component) getClient()) == JFileChooser.APPROVE_OPTION){
		    	dir = chooser.getSelectedFile().getAbsolutePath();
		    	getClient().sendMsgToServer(new MessageGetGradeTaskFile(task.getIdTask(), reg.getIdLesson(), user.getIdUser()));
			    MessageGetFile msg = (MessageGetFile) getClient().getMessageFromServer();  
			    try {
			    		// write the inputStream to a FileOutputStream
			    		OutputStream out = new FileOutputStream(new File(dir+File.separator+msg.getFile().getFileName()));
			    		out.write(msg.getFile().getBlob().getBytes(1, msg.getFile().getSize()));
			    		out.close();
			    		System.out.println("New file created!");
			    		JOptionPane.showMessageDialog((Component) getClient(), "Download complete");
			    	    } catch (IOException e1) {
			    		System.out.println(e1.getMessage());
			    	    } catch (SerialException e2) {
							e2.printStackTrace();
						}
			}
		}
	}


	@Override
	public MyJPanel pushPanel() {
		return new PersonalFilePanel(getClient(), user);
	}
}